﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Facade;
using FbKPI.Model;


namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for DownloadHandler
    /// </summary>
    public class DownloadHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            int KPIAttachmentID = Convert.ToInt32(context.Request.Params["KPIAttachmentID"]);
            KPIAttachmentFacade attFacade = new KPIAttachmentFacade();
            KPIAttachment objAtt = attFacade.GetKPIAttachmentById(KPIAttachmentID);

            if (objAtt != null)
            {

                System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream);
                sr.Close();
                sr.Dispose();

                context.Response.Clear();
                context.Response.Buffer = true;

                string filename = context.Server.MapPath("~" +objAtt.DocumentNotes);
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(filename);

                try
                {
                    if (fileInfo.Exists)
                    {
                        string nameFile = string.Empty;
                        string namaLokasi = string.Empty;






                        //Byte[] load = (Byte[])res.AttachFile;
                        if (objAtt.DocumentFileName.ToLower().EndsWith(".pdf"))
                            context.Response.ContentType = "application/pdf";

                        if (objAtt.DocumentFileName.ToLower().EndsWith(".xls"))
                            context.Response.ContentType = "application/vnd.ms-excel";

                        if (objAtt.DocumentFileName.ToLower().EndsWith(".xls"))
                            context.Response.ContentType = "application/vnd.ms-excel";

                        if (objAtt.DocumentFileName.ToLower().EndsWith(".txt"))
                            context.Response.ContentType = "application/text";

                        if (objAtt.DocumentFileName.ToLower().EndsWith(".jpeg") || objAtt.DocumentFileName.ToLower().EndsWith(".jpg"))
                            context.Response.ContentType = "application/image";

                        if (objAtt.DocumentFileName.ToLower().EndsWith(".doc"))
                            context.Response.ContentType = "application/msword";


                        //else if (res.AttachFileName.ToLower().EndsWith(".xls"))
                        //    context.Response.ContentType = "application/vnd.ms-excel";
                        //else if (res.AttachFileName.ToLower().EndsWith(".txt"))
                        //    context.Response.ContentType = "application/text";
                        //else //if (res.AttachFileName.ToLower().EndsWith(".jpeg") || res.AttachFileName.ToLower().EndsWith(".jpg"))
                        //    context.Response.ContentType = "application/image";
                        //else
                        //    context.Response.ContentType = "application/msword";


                        context.Response.Clear();
                        context.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileInfo.Name + "\"");
                        context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                        context.Response.ContentType = "application/octet-stream";
                        context.Response.TransmitFile(fileInfo.FullName);
                        context.Response.Flush();

                    }
                    else
                    {
                        throw new Exception("File not found");
                    }
                }
                catch (Exception ex)
                {
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(ex.Message);
                }
                finally
                {
                    context.Response.End();
                }
            }
            else
            {
                throw new Exception("File not found");
            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}